package com.jd.seckill;

import cn.hutool.core.collection.CollectionUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 解析获取sk参数
 * <p>
 * 规则如下：
 * 参数：[N,O,P,Q,R,S]
 * cca:P['slice'](14, 19)[\'toLowerCase\']()+ O[\'substring\'](5, 15)['toUpperCase\']()
 * ab:R['substring'](10, 18)+ S['toLowerCase']()['substring'](2, 13)
 * ch:Q[\'toUpperCase\']() + R[\'substring\'](6, 10)['toUpperCase\']()
 * cbc:Q[\'toUpperCase\']()['substring'](3, 13)+ P['toLowerCase\']()['substring'](10, 19)
 * by:O['substring'](5, 8) + P[\'replace\'](/a/gi, \'c\')
 * xa:O[\'substring\'](1, 16), S[\'slice\'](4, 10)
 * cza:Q['toLowerCase\']()['substring'](6, 19) + S['substring'](5, 11)
 * cb:S['substring'](5, 14)+P['substring'](2, 13)['toUpperCase\']()
 *
 * @author: xzl
 * @date: 2021/3/11
 */
public enum ESk {

    CCA("cca") {
        @Override
        public String getSk(LinkedHashMap<String, String> takMap) {
            String result = null;
            if (CollectionUtil.isNotEmpty(takMap)) {
                List<String> values = new ArrayList<>(takMap.values());
                result = values.get(2).substring(14, 19).toLowerCase() + values.get(1).substring(5, 15).toUpperCase();
            }
            return result;
        }
    },
    CZA("cza") {
        @Override
        public String getSk(LinkedHashMap<String, String> takMap) {
            String result = null;
            if (CollectionUtil.isNotEmpty(takMap)) {
                List<String> values = new ArrayList<>(takMap.values());
                result = values.get(3).substring(6, 19).toLowerCase() + values.get(5).substring(5, 11);
            }
            return result;
        }
    },
    CBC("cbc") {
        @Override
        public String getSk(LinkedHashMap<String, String> takMap) {
            String result = null;
            if (CollectionUtil.isNotEmpty(takMap)) {
                List<String> values = new ArrayList<>(takMap.values());
                result = values.get(3).substring(3, 13).toUpperCase() + values.get(2).substring(10, 19).toLowerCase();
            }
            return result;
        }
    },
    AB("ab") {
        @Override
        public String getSk(LinkedHashMap<String, String> takMap) {
            String result = null;
            if (CollectionUtil.isNotEmpty(takMap)) {
                List<String> values = new ArrayList<>(takMap.values());
                result = values.get(4).substring(10, 18) + values.get(5).substring(2, 13).toLowerCase();
            }
            return result;
        }
    },
    CH("ch") {
        @Override
        public String getSk(LinkedHashMap<String, String> takMap) {
            String result = null;
            if (CollectionUtil.isNotEmpty(takMap)) {
                List<String> values = new ArrayList<>(takMap.values());
                result = values.get(3).toUpperCase() + values.get(4).substring(6, 10).toUpperCase();
            }
            return result;
        }
    },
    BY("by") {
        @Override
        public String getSk(LinkedHashMap<String, String> takMap) {
            String result = null;
            if (CollectionUtil.isNotEmpty(takMap)) {
                List<String> values = new ArrayList<>(takMap.values());
                result = values.get(1).substring(5, 8) + values.get(2).replaceAll("a|A", "c");
            }
            return result;
        }
    },
    XA("xa") {
        @Override
        public String getSk(LinkedHashMap<String, String> takMap) {
            String result = null;
            if (CollectionUtil.isNotEmpty(takMap)) {
                List<String> values = new ArrayList<>(takMap.values());
                result = values.get(1).substring(1, 16) + values.get(5).substring(4, 10);
            }
            return result;
        }
    },
    CB("cb") {
        @Override
        public String getSk(LinkedHashMap<String, String> takMap) {
            String result = null;
            if (CollectionUtil.isNotEmpty(takMap)) {
                List<String> values = new ArrayList<>(takMap.values());
                result = values.get(5).substring(5, 14) + values.get(2).substring(2, 13).toUpperCase();
            }
            return result;
        }
    };

    private String type;

    ESk(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public ESk get(String type) {
        ESk result = null;
        if (null != type) {
            ESk[] values = ESk.values();
            for (ESk value : values) {
                if (value.getType().equals(type)) {
                    result = value;
                    break;
                }
            }

        }
        return result;
    }


    public abstract String getSk(LinkedHashMap<String, String> takMap);
}
